<?php
class CIPerson {

	public $Type;
	public $Id;
	public $relatedData;
	public $matchingAccounts;
	public $Email;
	public $cacheHit;
	public $doLookup;
	
	public function __construct($sObject=null) {
		if ($sObject == null) return;
		
		$user = new InsightUser();
		
		$this->Type = $sObject->type;
		$this->Id = $sObject->Id;
		$this->relatedData = array();
		
		if ($this->Type == "Contact" && isset($sObject->fields->Account->Id)) {
			$this->AccountId = $sObject->fields->Account->Id;
		}

		// parse data from lead/contact fields
		$config = $user->getObjConfig($sObject->type);
		$this->detail = CIPerson::parseSObject($sObject, $config);
		$this->url = $user->getRecordUrl($sObject->Id);
		
		// parse data from related lists
		$layout = $user->getLayout($sObject);
		foreach ($layout->relatedLists as $related_list) {
			if (!$user->hasRelatedList($this->Type, $related_list->name)) continue;

			$list = new stdClass();
				$list->label = $related_list->label;
				$list->records = array();
				$list->id = $related_list->name;
			
			if (isset($sObject->queryResult)) {
				// one QueryResult for each related list
				foreach ($sObject->queryResult as $data) {
					// run through the results until we find the one that matches the related list config
					if ($data->records[0]->type == $related_list->sobject) { 
						$list->records = $data->records;
	
						// get the record URL
						// use the first column value as the name displayed in the related list
						foreach ($list->records as $rec) {
							$rec->url = $user->getRecordUrl($rec->Id);
							$fields = array();
	
							foreach ($related_list->columns as $col) {
								$field = $col->name;
								$field = CIPerson::cleanLabel($field);
								
								// if the field contains a '.' it's referencing a field in a related object 
								// and should be handled as an sObject 
								if (stripos($field, '.') !== FALSE) {
									list($object, $field) = explode('.', $field, 2);
									$o = $rec->fields->$object;
									if (isset($o->fields)) {
										$col->value = $o->fields->$field;
									}
								}
								
								// treat it as a standard field
								else {
									if (isset($rec->fields)) {
										$col->value = $rec->fields->$field;
										
										if ($col->name == 'Description' && (strtolower($related_list->name) == 'activityhistories' || strtolower($related_list->name) == 'openactivities')) {
											// replace newlines with <br/>
											$col->value = str_replace("\n", "<br/>", $col->value); 
										}
									}
								}
								
								// set the name
								if (!isset($rec->Name)) $rec->Name = $col->value;
								$fld = new stdClass();
									if (isset($col->label)) $fld->label = $col->label;
									if (isset($col->value)) $fld->value = $col->value;
									if (isset($col->name)) $fld->name = CIPerson::cleanLabel($col->name);
									
								$fields[] = $fld;
							}
							
							if ($rec->type == "Case") {
								foreach ($fields as $f) {
									if ($f->label == "Subject") {
										$rec->Name .= " - ".$f->value;
										break;
									}
								}
							}
							
							$rec->fields = $fields;
						}
						break;
					}
				}
			}
	
			$this->relatedData[] = $list;
		}

		$this->cacheHit = false;
	}
	
	private static function cleanLabel($label) {
		if (stripos($label, 'toLabel') !== FALSE) {
			$label = str_replace('toLabel(', '', $label);
			$label = str_replace(')', '', $label);
		}
		return $label;
	}
	
	public function getMatchingAccounts($email) {
		$this->matchingAccounts = Salesforce::getMatchingAccounts($email);
	}
	
	private static function parseSObject($sobj, $config) {
		$user = new InsightUser();

		foreach ($config as $cfg) {
			$field = $cfg->name;

			if (isset($cfg->type) && $cfg->type == 'special') {
				continue;

			} else if (isset($cfg->type) && $cfg->type == 'hover' && isset($sobj->fields->$field)) {
				// we know this is a new sObject
				$child_obj = $sobj->fields->$field;
				$cfg->fields = CIPerson::parseSObject($child_obj, $cfg->fields);
				$cfg->url = $user->getRecordUrl($child_obj->Id);
				$cfg->value = $child_obj->fields->Name;
				$cfg->label = $_SESSION[DESCRIBE_KEY][strtolower($child_obj->type)]->label;

			} else if ($field == 'OwnerId' && isset($sobj->fields->Owner)) {
				$owner_config = new stdClass(); $owner_config->name = "Name";
				$owner = CIPerson::parseSObject($sobj->fields->Owner, array($owner_config));
				$cfg->label = "Owner"; // change from "Owner ID"; note - isn't localized
				$cfg->value = $owner[0]->value;

			} else if (isset($sobj->fields->$field)) {
				$cfg->value = $sobj->fields->$field;
 
			} else if (strtolower($field) == 'id') { 
				$cfg->value = $sobj->Id;
			}
		}
		
		return $config;
	}
}
?>